<!DOCTYPE html>
<html>
  
<!-- Mirrored from www.dba.cn/book/electron/ZaiZhuJinChengNeiKeYongDeMoKuai/ELECTRONAPPMoKuai.html by HTTrack Website Copier/3.x [XR&CO'2014], Sun, 20 Mar 2022 10:24:08 GMT -->
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Electron app 模块 - Electron中文手册</title>
    <link rel="stylesheet" type="text/css" href="../../skins/cms/css/min-easyui.css">
    <link rel="stylesheet" type="text/css" href="../../skins/cms/css/book.css">
    <script type="text/javascript" src="../../js/jquery.min.js"></script>
    <script type="text/javascript" src="../../skins/cms/js/m-easyui.js"></script>
    <script type="text/javascript" src="../../skins/cms/js/prettify.js"></script>
  </head>
  <body class="easyui-layout" style="text-align:left">
    <div region="north" border="false" class="group wrap header" style="height:66px;font-size:100%">
      <div class="header">
        <div class="navigation-toggle" data-tools="navigation-toggle" data-target="#navbar-1">
          <span>Electron中文手册</span>
        </div>
        <div id="elogo" class="navbar navbar-left">
          <ul>
            <li>
              <h1><a href="../index.html">Electron中文手册</a></h1>
            </li>
          </ul>
        </div>
      <div style="clear:both"></div>
    </div>
   </div>
    <div region="west" split="true" data-options="collapsedSize:0,hideExpandTool:true" title="文档目录" style="width:20%;min-width:300px;padding:5px;display:none;">
        
      <ul class="easyui-tree"> 
        <li iconcls="icon-base"><span>Electron 教程</span><ul><li iconcls="icon-gears"><a href="../ELECTRONJiaoCheng/ELECTRONJiaoChengJianJie.html">Electron 教程简介</a></li><li iconcls="icon-gears"><a  href="../ELECTRONJiaoCheng/ELECTRONKuaiSuRuMen.html">Electron 快速入门</a></li><li iconcls="icon-gears"><a  href="../ELECTRONJiaoCheng/ELECTRONZhuoMianHuanJingJiCheng.html">Electron 桌面环境集成</a></li><li iconcls="icon-gears"><a  href="../ELECTRONJiaoCheng/ELECTRONZaiXianLiXianShiJianTanCe.html">Electron 在线/离线事件探测</a></li><li iconcls="icon-gears"><a  href="../ELECTRONJiaoCheng/ELECTRONJinCheng.html">Electron 进程</a></li><li iconcls="icon-gears"><a  href="../ELECTRONJiaoCheng/ELECTRONZhiChiDeCHROMEMingLingXingKaiGuan.html">Electron 支持的 Chrome 命令行开关</a></li><li iconcls="icon-gears"><a  href="../ELECTRONJiaoCheng/ELECTRONHuanJingBianLiang.html">Electron 环境变量</a></li><li iconcls="icon-gears"><a  href="../ELECTRONJiaoCheng/ELECTRONZhiChiDePingTai.html">Electron 支持的平台</a></li><li iconcls="icon-gears"><a  href="../ELECTRONJiaoCheng/ELECTRONYingYongBuShu.html">Electron 应用部署</a></li><li iconcls="icon-gears"><a  href="../ELECTRONJiaoCheng/ELECTRONXiangMACAPPSTOREYingYongTiJiaoXiangDao.html">Electron 向Mac App Store 应用提交向导</a></li><li iconcls="icon-gears"><a  href="../ELECTRONJiaoCheng/ELECTRONYingYongDaBao.html">Electron 应用打包</a></li><li iconcls="icon-gears"><a  href="../ELECTRONJiaoCheng/ELECTRONShiYongYuanShengMoKuai.html">Electron 使用原生模块</a></li><li iconcls="icon-gears"><a  href="../ELECTRONJiaoCheng/ELECTRONZhuJinChengDiaoShi.html">Electron 主进程调试</a></li><li iconcls="icon-gears"><a  href="../ELECTRONJiaoCheng/ELECTRONShiYongSELENIUMHeWEBDRIVER.html">Electron 使用 Selenium 和 WebDriver</a></li><li iconcls="icon-gears"><a  href="../ELECTRONJiaoCheng/ELECTRONDEVTOOLSKuoZhan.html">Electron DevTools扩展</a></li><li iconcls="icon-gears"><a  href="../ELECTRONJiaoCheng/ELECTRONShiYongPEPPERFLASHChaJian.html">Electron 使用 Pepper Flash 插件</a></li><li iconcls="icon-gears"><a  href="../ELECTRONJiaoCheng/ELECTRONShiYongWIDEVINECDMChaJian.html">Electron 使用 Widevine CDM 插件</a></li><li iconcls="icon-gears"><a  href="../ELECTRONJiaoCheng/ELECTRONShuYuBiao.html">Electron 术语表</a></li><li iconcls="icon-gears"><a  href="../ELECTRONJiaoCheng/ELECTRONLiPingRan.html">Electron 离屏渲染</a></li><li iconcls="icon-gears"><a  href="../ELECTRONJiaoCheng/ELECTRONJiaoHuShiJieShiQiREPL.html">Electron 交互式解释器 (REPL)</a></li></ul></li><li iconcls="icon-base"><span>Electron API</span><ul><li iconcls="icon-gears"><a  href="../ELECTRONAPI/ELECTRONDOMFILEDuiXiang.html">Electron DOM File对象</a></li><li iconcls="icon-gears"><a  href="../ELECTRONAPI/ELECTRONDOMWEBVIEWBiaoQian.html">Electron DOM <webview> 标签</a></li><li iconcls="icon-gears"><a  href="../ELECTRONAPI/ELECTRONWINDOWOPENHanShu.html">Electron window.open 函数</a></li></ul></li><li iconcls="icon-base"><span>在主进程内可用的模块</span><ul><li iconcls="icon-gears"><a  href="ELECTRONAPPMoKuai.html">Electron app 模块</a></li><li iconcls="icon-gears"><a  href="ELECTRONAUTOUPDATERMoKuai.html">Electron autoUpdater 模块</a></li><li iconcls="icon-gears"><a  href="ELECTRONBROWSERWINDOWMoKuai.html">Electron BrowserWindow 模块</a></li><li iconcls="icon-gears"><a  href="ELECTRONCONTENTTRACINGMoKuai.html">Electron contentTracing 模块</a></li><li iconcls="icon-gears"><a  href="ELECTRONDIALOGMoKuai.html">Electron dialog 模块</a></li><li iconcls="icon-gears"><a  href="ELECTRONGLOBAL-SHORTCUTMoKuai.html">Electron global-shortcut 模块</a></li><li iconcls="icon-gears"><a  href="ELECTRONIPCMAINMoKuai.html">Electron ipcMain 模块</a></li><li iconcls="icon-gears"><a  href="ELECTRONMENUMoKuai.html">Electron menu 模块</a></li><li iconcls="icon-gears"><a  href="ELECTRONMENUITEMMoKuai.html">Electron MenuItem 模块</a></li><li iconcls="icon-gears"><a  href="ELECTRONPOWERMONITORMoKuai.html">Electron powerMonitor 模块</a></li><li iconcls="icon-gears"><a  href="ELECTRONPOWERSAVEBLOCKERMoKuai.html">Electron powerSaveBlocker 模块</a></li><li iconcls="icon-gears"><a  href="ELECTRONPROTOCOLMoKuai.html">Electron protocol 模块</a></li><li iconcls="icon-gears"><a  href="ELECTRONSESSIONMoKuai.html">Electron session 模块</a></li><li iconcls="icon-gears"><a  href="ELECTRONWEBCONTENTSMoKuai.html">Electron webContents 模块</a></li><li iconcls="icon-gears"><a  href="ELECTRONTRAYMoKuai.html">Electron Tray 模块</a></li><li iconcls="icon-gears"><a  href="ELECTRONLOCALES.html">Electron Locales</a></li></ul></li><li iconcls="icon-base"><span>在渲染进程（网页）内可用的模块</span><ul><li iconcls="icon-gears"><a  href="../ZaiRanJinChengWangYeNeiKeYongDeMoKuai/ELECTRONDESKTOPCAPTURERMoKuai.html">Electron desktopCapturer 模块</a></li><li iconcls="icon-gears"><a  href="../ZaiRanJinChengWangYeNeiKeYongDeMoKuai/ELECTRONIPCRENDERERMoKuai.html">Electron ipcRenderer 模块</a></li><li iconcls="icon-gears"><a  href="../ZaiRanJinChengWangYeNeiKeYongDeMoKuai/ELECTRONREMOTEMoKuai.html">Electron remote 模块</a></li><li iconcls="icon-gears"><a  href="../ZaiRanJinChengWangYeNeiKeYongDeMoKuai/ELECTRONWEBFRAMEMoKuai.html">Electron webFrame 模块</a></li></ul></li><li iconcls="icon-base"><span>两种进程都可用的模块</span><ul><li iconcls="icon-gears"><a  href="../LiangZhongJinChengDuKeYongDeMoKuai/ELECTRONCLIPBOARDMoKuai.html">Electron clipboard 模块</a></li><li iconcls="icon-gears"><a  href="../LiangZhongJinChengDuKeYongDeMoKuai/ELECTRONCRASHREPORTERMoKuai.html">Electron crashReporter 模块</a></li><li iconcls="icon-gears"><a  href="../LiangZhongJinChengDuKeYongDeMoKuai/ELECTRONNATIVEIMAGEMoKuai.html">Electron nativeImage 模块</a></li><li iconcls="icon-gears"><a  href="../LiangZhongJinChengDuKeYongDeMoKuai/ELECTRONSCREENMoKuai.html">Electron screen 模块</a></li><li iconcls="icon-gears"><a  href="../LiangZhongJinChengDuKeYongDeMoKuai/ELECTRONSHELLMoKuai.html">Electron shell 模块</a></li></ul></li><li iconcls="icon-base"><span>Electron 开发</span><ul><li iconcls="icon-gears"><a  href="../ELECTRONKaiFa/ELECTRONBianMaGuiFan.html">Electron 编码规范</a></li><li iconcls="icon-gears"><a  href="../ELECTRONKaiFa/ELECTRONYuanMaMuLuJieGou.html">Electron 源码目录结构</a></li><li iconcls="icon-gears"><a  href="../ELECTRONKaiFa/ELECTRONHeNWJSYuanMingNODE-WEBKITZaiJiShuShangDeChaYi.html">Electron 和 NW.js (原名 node-webkit) 在技术上的差异</a></li><li iconcls="icon-gears"><a  href="../ELECTRONKaiFa/ELECTRONGouJianXiTongGaiLan.html">Electron 构建系统概览</a></li><li iconcls="icon-gears"><a  href="../ELECTRONKaiFa/ELECTRONGouJianBuZhouOSX.html">Electron 构建步骤 (OS X)</a></li><li iconcls="icon-gears"><a  href="../ELECTRONKaiFa/ELECTRONGouJianBuZhouWINDOWS.html">Electron 构建步骤 (Windows)</a></li><li iconcls="icon-gears"><a  href="../ELECTRONKaiFa/ELECTRONGouJianBuZhouLINUX.html">Electron 构建步骤 (Linux)</a></li><li iconcls="icon-gears"><a  href="../ELECTRONKaiFa/ELECTRONZaiDiaoShiZhongShiYongSYMBOLSERVER.html">Electron 在调试中使用 Symbol Server</a></li><li iconcls="icon-gears"><a  href="../ELECTRONKaiFa/ELECTRONChangJianWenTi.html">Electron 常见问题</a></li><li iconcls="icon-gears"><a  href="../ELECTRONKaiFa/ELECTRONBanBenGuanLi.html">Electron 版本管理</a></li><li iconcls="icon-gears"><a  href="../ELECTRONKaiFa/ELECTRONWINDOWTiJiaoZhiNan.html">electron window 提交指南</a></li><li iconcls="icon-gears"><a  href="../ELECTRONKaiFa/ZiDongHuaChiXuJiChengXiTongCICeShi.html">自动化持续集成系统（CI）测试</a></li><li iconcls="icon-gears"><a  href="../ELECTRONKaiFa/ELECTRONWenDangFengGeZhiNan.html">Electron 文档风格指南</a></li></ul></li>
      </ul>
    
    </div>
    <div region="center">
      <div id="tt" class="easyui-tabs" fit="true" border="false" plain="true">
        <div title="Electron app 模块 - Electron中文手册">  
            <div class="book-content">
              
      <div style="padding:8px;"> &gt; <a href="../index.html">Electron中文手册</a> &gt; Electron app 模块</div>
      <div class="content"><p class="comments-section" ><span >app</span> 模块是为了控制整个应用的生命周期设计的。</p>
<p class="comments-section" >下面的这个例子将会展示如何在最后一个窗口被关闭时退出应用：</p>
<pre ><span >var<span > app = </span></span><span >require</span>(<span >'app'</span>);
app.on(<span >'window-all-closed'</span>, <span >function<span >(</span></span>) {
  app.quit();
});
</pre>
<h2 id="事件列表" >事件列表</h2>
<p class="comments-section" ><span >app</span> 对象会触发以下的事件：</p>
<h3 id="事件：willfinishlaunching" >事件：'will-finish-launching'</h3>
<p class="comments-section" >当应用程序完成基础的启动的时候被触发。在 Windows 和 Linux 中，<span> </span><span ><span >will-finish-launching</span></span><span> </span>事件与<span> </span><span ><span >ready</span></span><span> </span>事件是相同的； 在 OS X 中， 这个时间相当于<span> </span><span ><span >NSApplication</span></span><span> </span>中的<span> </span><span ><span >applicationWillFinishLaunching</span></span><span> </span>提示。 你应该经常在这里为<span> </span><span ><span >open-file</span></span><span> </span>和<span> </span><span ><span >open-url</span></span><span> </span>设置监听器，并启动崩溃报告和自动更新。</p>
<p class="comments-section" >在大多数的情况下，你应该只在<span> </span><span ><span >ready</span></span><span> </span>事件处理器中完成所有的业务。</p>
<h3 id="事件：ready" >事件：'ready'</h3>
<p class="comments-section" >当 Electron 完成初始化时被触发。</p>
<h3 id="事件：windowallclosed" >事件：'window-all-closed'</h3>
<p class="comments-section" >当所有的窗口都被关闭时触发。</p>
<p class="comments-section" >这个时间仅在应用还没有退出时才能触发。 如果用户按下了<span> </span><span ><span >Cmd + Q</span></span>， 或者开发者调用了<span> </span><span ><span >app.quit()</span></span><span> </span>，Electron 将会先尝试关闭所有的窗口再触发<span> </span><span ><span >will-quit</span></span><span> </span>事件， 在这种情况下<span> </span><span ><span >window-all-closed</span></span><span> </span>不会被触发。</p>
<h3 id="事件：beforequit" >事件：'before-quit'</h3>
<p >返回：</p>
<ul ><li >
<span ><span >event</span></span><span> </span>Event</li></ul>
<p class="comments-section" >在应用程序开始关闭它的窗口的时候被触发。 调用<span> </span><span ><span >event.preventDefault()</span></span><span> </span>将会阻止终止应用程序的默认行为。</p>
<h3 id="事件：willquit" >事件：'will-quit'</h3>
<p >返回：</p>
<ul ><li >
<span ><span >event</span></span><span> </span>Event</li></ul>
<p class="comments-section" >当所有的窗口已经被关闭，应用即将退出时被触发。 调用<span> </span><span ><span >event.preventDefault()</span></span><span> </span>将会阻止终止应用程序的默认行为。</p>
<p class="comments-section" >你可以在<span> </span><span ><span >window-all-closed</span></span><span> </span>事件的描述中看到<span> </span><span ><span >will-quit</span></span><span> </span>事件 和<span> </span><span ><span >window-all-closed</span></span><span> </span>事件的区别。</p>
<h3 id="事件：quit" >事件：'quit'</h3>
<p >返回：</p>
<ul >
<li >
<span ><span >event</span></span><span> </span>Event</li>
<li >
<span ><span >exitCode</span></span><span> </span>Integer</li>
</ul>
<p class="comments-section" >当应用程序正在退出时触发。</p>
<h3 id="事件：openfile-os-x" >事件：'open-file'<span> </span><span >OS X</span>
</h3>
<p >返回：</p>
<ul >
<li >
<span ><span >event</span></span><span> </span>Event</li>
<li >
<span ><span >path</span></span><span> </span>String</li>
</ul>
<p class="comments-section" >当用户想要在应用中打开一个文件时触发。<span ><span >open-file</span></span><span> </span>事件常常在应用已经打开并且系统想要再次使用应用打开文件时被触发。<span> </span><span ><span >open-file</span></span><span> </span>也会在一个文件被拖入 dock 且应用还没有运行的时候被触发。 请确认在应用启动的时候（甚至在<span> </span><span ><span >ready</span></span><span> </span>事件被触发前）就对<span> </span><span ><span >open-file</span></span><span> </span>事件进行监听，以处理这种情况。</p>
<p class="comments-section" >如果你想处理这个事件，你应该调用<span> </span><span ><span >event.preventDefault()</span></span><span> </span>。 在 Windows系统中，你需要通过解析 process.argv 来获取文件路径。</p>
<h3 id="事件：openurl-os-x" >事件：'open-url'<span> </span><span >OS X</span>
</h3>
<p >返回：</p>
<ul >
<li >
<span ><span >event</span></span><span> </span>Event</li>
<li >
<span ><span >url</span></span><span> </span>String</li>
</ul>
<p class="comments-section" >当用户想要在应用中打开一个url的时候被触发。URL格式必须要提前标识才能被你的应用打开。</p>
<p class="comments-section" >如果你想处理这个事件，你应该调用<span> </span><span ><span >event.preventDefault()</span></span><span> </span>。</p>
<h3 id="事件：activate-os-x" >事件：'activate'<span> </span><span >OS X</span>
</h3>
<p >返回：</p>
<ul >
<li >
<span ><span >event</span></span><span> </span>Event</li>
<li >
<span ><span >hasVisibleWindows</span></span><span> </span>Boolean</li>
</ul>
<p class="comments-section" >当应用被激活时触发，常用于点击应用的 dock 图标的时候。</p>
<h3 id="事件：browserwindowblur" >事件：'browser-window-blur'</h3>
<p >返回：</p>
<ul >
<li >
<span ><span >event</span></span><span> </span>Event</li>
<li >
<span ><span >window</span></span><span> </span>BrowserWindow</li>
</ul>
<p class="comments-section" >当一个<span> </span><a href="BROWSERWINDOW.html">BrowserWindow</a><span> </span>失去焦点的时候触发。</p>
<h3 id="事件：browserwindowfocus" >事件：'browser-window-focus'</h3>
<p >返回：</p>
<ul >
<li >
<span ><span >event</span></span><span> </span>Event</li>
<li >
<span ><span >window</span></span><span> </span>BrowserWindow</li>
</ul>
<p class="comments-section" >当一个<span> </span><a href="BROWSERWINDOW.html">BrowserWindow</a><span> </span>获得焦点的时候触发。</p>
<h3 id="事件：browserwindowcreated" >事件：'browser-window-created'</h3>
<p >返回：</p>
<ul >
<li >
<span ><span >event</span></span><span> </span>Event</li>
<li >
<span ><span >window</span></span><span> </span>BrowserWindow</li>
</ul>
<p class="comments-section" >当一个<span> </span><a href="BROWSERWINDOW.html">BrowserWindow</a><span> </span>被创建的时候触发。</p>
<h3 id="事件：certificateerror" >事件：'certificate-error'</h3>
<p >返回：</p>
<ul >
<li >
<span ><span >event</span></span><span> </span>Event</li>
<li >
<span ><span >webContents</span></span><span> </span><a href="WEBCONTENTS.html">WebContents</a>
</li>
<li >
<span ><span >url</span></span><span> </span>String - URL 地址</li>
<li >
<span ><span >error</span></span><span> </span>String - 错误码</li>
<li >
<span ><span >certificate</span></span><span> </span>Object</li>
<li >
<span ><span >data</span></span><span > </span><span >Buffer - PEM 编码数据</span>
</li>
<li >
<span ><span >issuerName</span></span><span > </span><span >String - 发行者的公有名称</span>
</li>
<li >
<span ><span >callback</span></span><span> </span>Function</li>
</ul>
<p class="comments-section" >当对<span> </span><span ><span >url</span></span><span> </span>验证<span> </span><span ><span >certificate</span></span><span> </span>证书失败的时候触发，如果需要信任这个证书，你需要阻止默认行为<span> </span><span ><span >event.preventDefault()</span></span><span> </span>并且 调用<span> </span><span ><span >callback(true)</span></span>。</p>
<pre ><span >session.on(</span><span >'certificate-error'</span>, <span >function<span >(</span>event, webContents, url, error, certificate, callback</span>) {
  <span >if</span> (url == <span >"https://github.com"</span>) {
    <span >// 验证逻辑。</span>
    event.preventDefault();
    callback(<span >true</span>);
  } <span >else</span> {
    callback(<span >false</span>);
  }
});
</pre>
<h3 id="事件：selectclientcertificate" >事件：'select-client-certificate'</h3>
<p >返回：</p>
<ul >
<li >
<span ><span >event</span></span><span> </span>Event</li>
<li >
<span ><span >webContents</span></span><span> </span><a href="WEBCONTENTS.html">WebContents</a>
</li>
<li >
<span ><span >url</span></span><span> </span>String - URL 地址</li>
<li >
<span ><span >certificateList</span></span><span> </span>[Object]</li>
<li >
<span ><span >data</span></span><span > </span><span >Buffer - PEM 编码数据</span>
</li>
<li >
<span ><span >issuerName</span></span><span > </span><span >String - 发行者的公有名称</span>
</li>
<li >
<span ><span >callback</span></span><span> </span>Function</li>
</ul>
<p class="comments-section" >当一个客户端认证被请求的时候被触发。</p>
<p class="comments-section" ><span ><span >url</span></span><span> </span>指的是请求客户端认证的网页地址，调用<span> </span><span ><span >callback</span></span><span> </span>时需要传入一个证书列表中的证书。</p>
<p class="comments-section" >需要通过调用<span> </span><span ><span >event.preventDefault()</span></span><span> </span>来防止应用自动使用第一个证书进行验证。如下所示：</p>
<pre ><span >app.on(</span><span >'select-certificate'</span>, <span >function<span >(</span>event, host, url, list, callback</span>) {
  event.preventDefault();
  callback(list[<span >0</span>]);
})
</pre>
<h3 id="事件-login" >事件: 'login'</h3>
<p >返回：</p>
<ul >
<li >
<span ><span >event</span></span><span> </span>Event</li>
<li >
<span ><span >webContents</span></span><span> </span><a href="WEBCONTENTS.html">WebContents</a>
</li>
<li >
<span ><span >request</span></span><span> </span>Object<ul >
<li >
<span ><span >method</span></span><span> </span>String</li>
<li >
<span ><span >url</span></span><span> </span>URL</li>
<li >
<span ><span >referrer</span></span><span> </span>URL</li>
</ul>
</li>
<li >
<span ><span >authInfo</span></span><span> </span>Object<ul >
<li >
<span ><span >isProxy</span></span><span> </span>Boolean</li>
<li >
<span ><span >scheme</span></span><span> </span>String</li>
<li >
<span ><span >host</span></span><span> </span>String</li>
<li >
<span ><span >port</span></span><span> </span>Integer</li>
<li >
<span ><span >realm</span></span><span> </span>String</li>
</ul>
</li>
<li >
<span ><span >callback</span></span><span> </span>Function</li>
</ul>
<p class="comments-section" >当<span> </span><span ><span >webContents</span></span><span> </span>要做进行一次 HTTP 登陆验证时被触发。</p>
<p class="comments-section" >默认情况下，Electron 会取消所有的验证行为，如果需要重写这个行为，你需要用<span> </span><span ><span >event.preventDefault()</span></span><span> </span>来阻止默认行为，并且 用<span> </span><span ><span >callback(username, password)</span></span><span> </span>来进行验证。</p>
<pre ><span >app.on(</span><span >'login'</span>, <span >function<span >(</span>event, webContents, request, authInfo, callback</span>) {
  event.preventDefault();
  callback(<span >'username'</span>, <span >'secret'</span>);
})
</pre>
<h3 id="事件：gpuprocesscrashed" >事件：'gpu-process-crashed'</h3>
<p class="comments-section" >当 GPU 进程崩溃时触发。</p>
<h2 id="方法列表" >方法列表</h2>
<p class="comments-section" ><span ><span >app</span></span><span> </span>对象拥有以下的方法：</p>
<p class="comments-section" ><span >请注意</span><span> </span>有的方法只能用于特定的操作系统。</p>
<h3 id="appquit" ><span ><span >app.quit()</span></span></h3>
<p class="comments-section" >试图关掉所有的窗口。<span ><span >before-quit</span></span><span> </span>事件将会最先被触发。如果所有的窗口都被成功关闭了，<span> </span><span ><span >will-quit</span></span>事件将会被触发，默认下应用将会被关闭。</p>
<p class="comments-section" >这个方法保证了所有的<span> </span><span ><span >beforeunload</span></span><span> </span>和<span> </span><span ><span >unload</span></span><span> </span>事件处理器被正确执行。假如一个窗口的<span> </span><span ><span >beforeunload</span></span>事件处理器返回<span> </span><span ><span >false</span></span>，那么整个应用可能会取消退出。</p>
<h3 id="apphide-os-x" >
<span ><span >app.hide()</span></span><span> </span><span >OS X</span>
</h3>
<p class="comments-section" >隐藏所有的应用窗口，不是最小化.</p>
<h3 id="appshow-os-x" >
<span ><span >app.show()</span></span><span> </span><span >OS X</span>
</h3>
<p class="comments-section" >隐藏后重新显示所有的窗口，不会自动选中他们。</p>
<h3 id="appexitexitcode" ><span ><span >app.exit(exitCode)</span></span></h3>
<ul ><li >
<span ><span >exitCode</span></span><span> </span>整数</li></ul>
<p class="comments-section" >带着<span ><span >exitCode</span></span>退出应用.</p>
<p class="comments-section" >所有的窗口会被立刻关闭，不会询问用户。<span ><span >before-quit</span></span><span> </span>和<span> </span><span ><span >will-quit</span></span><span> </span>这2个事件不会被触发</p>
<h3 id="appgetapppath" ><span ><span >app.getAppPath()</span></span></h3>
<p class="comments-section" >返回当前应用所在的文件路径。</p>
<h3 id="appgetpathname" ><span ><span >app.getPath(name)</span></span></h3>
<ul ><li >
<span ><span >name</span></span><span> </span>String</li></ul>
<p class="comments-section" >返回一个与<span> </span><span ><span >name</span></span><span> </span>参数相关的特殊文件夹或文件路径。当失败时抛出一个<span> </span><span ><span >Error</span></span><span> </span>。</p>
<p class="comments-section" >你可以通过名称请求以下的路径：</p>
<ul >
<li >
<span ><span >home</span></span><span> </span>用户的 home 文件夹（主目录）</li>
<li >
<span ><span >appData</span></span><span> </span>当前用户的应用数据文件夹，默认对应：<ul >
<li >
<span ><span >%APPDATA%</span></span><span> </span>Windows 中</li>
<li >
<span ><span >$XDG_CONFIG_HOME</span></span><span> </span>or<span> </span><span ><span >~/.config</span></span><span> </span>Linux 中</li>
<li >
<span ><span >~/Library/Application Support</span></span><span> </span>OS X 中</li>
</ul>
</li>
<li >
<span ><span >userData</span></span><span> </span>储存你应用程序设置文件的文件夹，默认是<span> </span><span ><span >appData</span></span><span> </span>文件夹附加应用的名称</li>
<li >
<span ><span >temp</span></span><span> </span>临时文件夹</li>
<li >
<span ><span >exe</span></span><span> </span>当前的可执行文件</li>
<li >
<span ><span >module</span><span> </span><span >libchromiumcontent</span></span><span> </span>库</li>
<li >
<span ><span >desktop</span></span><span> </span>当前用户的桌面文件夹</li>
<li >
<span ><span >documents</span></span><span> </span>用户文档目录的路径</li>
<li >
<span ><span >downloads</span></span><span> </span>用户下载目录的路径.</li>
<li >
<span ><span >music</span></span><span> </span>用户音乐目录的路径.</li>
<li >
<span ><span >pictures</span></span><span> </span>用户图片目录的路径.</li>
<li >
<span ><span >videos</span></span><span> </span>用户视频目录的路径.</li>
</ul>
<h3 id="appsetpathname-path" ><span ><span >app.setPath(name, path)</span></span></h3>
<ul >
<li >
<span ><span >name</span></span><span> </span>String</li>
<li >
<span ><span >path</span></span><span> </span>String</li>
</ul>
<p class="comments-section" >重写某个<span> </span><span ><span >name</span></span><span> </span>的路径为<span> </span><span ><span >path</span></span>，<span ><span >path</span></span><span> </span>可以是一个文件夹或者一个文件，这个和<span> </span><span ><span >name</span></span><span> </span>的类型有关。 如果这个路径指向的文件夹不存在，这个文件夹将会被这个方法创建。 如果错误则会抛出<span> </span><span ><span >Error</span></span>。</p>
<p class="comments-section" ><span ><span >name</span></span><span> </span>参数只能使用<span> </span><span ><span >app.getPath</span></span><span> </span>中定义过<span> </span><span ><span >name</span></span>。</p>
<p class="comments-section" >默认情况下，网页的 cookie 和缓存都会储存在<span> </span><span ><span >userData</span></span><span> </span>文件夹。 如果你想要改变这个位置，你需要在<span> </span><span ><span >app</span></span><span> </span>模块中的<span> </span><span ><span >ready</span></span><span> </span>事件被触发之前重写<span> </span><span ><span >userData</span></span><span> </span>的路径。</p>
<h3 id="appgetversion" ><span ><span >app.getVersion()</span></span></h3>
<p class="comments-section" >返回加载应用程序的版本。如果应用程序的<span> </span><span ><span >package.json</span></span><span> </span>文件中没有写版本号， 将会返回当前包或者可执行文件的版本。</p>
<h3 id="appgetname" ><span ><span >app.getName()</span></span></h3>
<p class="comments-section" >返回当前应用程序的<span> </span><span ><span >package.json</span></span><span> </span>文件中的名称。</p>
<p class="comments-section" >由于 npm 的命名规则，通常<span> </span><span ><span >name</span></span><span> </span>字段是一个短的小写字符串。但是应用名的完整名称通常是首字母大写的，你应该单独使用一个<span> </span><span ><span >productName</span></span><span> </span>字段，用于表示你的应用程序的完整名称。Electron 会优先使用这个字段作为应用名。</p>
<h3 id="appsetnamename" ><span ><span >app.setName(name)</span></span></h3>
<ul ><li >
<span ><span >name</span></span><span> </span>String</li></ul>
<p class="comments-section" >重写当前应用的名字</p>
<h3 id="appgetlocale" ><span ><span >app.getLocale()</span></span></h3>
<p class="comments-section" >返回当前应用程序的语言。</p>
<h3 id="appaddrecentdocumentpath--os-x-windows" >
<span ><span >app.addRecentDocument(path)</span></span><span> </span><span >OS X</span> <span >Windows</span>
</h3>
<ul ><li >
<span ><span >path</span></span><span> </span>String</li></ul>
<p class="comments-section" >在最近访问的文档列表中添加<span> </span><span ><span >path</span></span>。</p>
<p class="comments-section" >这个列表由操作系统进行管理。在 Windows 中您可以通过任务条进行访问，在 OS X 中你可以通过 dock 菜单进行访问。</p>
<h3 id="appclearrecentdocuments-os-x-windows" >
<span ><span >app.clearRecentDocuments()</span></span><span> </span><span >OS X</span> <span >Windows</span>
</h3>
<p class="comments-section" >清除最近访问的文档列表。</p>
<h3 id="appsetusertaskstasks-windows" >
<span ><span >app.setUserTasks(tasks)</span></span><span> </span><span >Windows</span>
</h3>
<ul ><li >
<span ><span >tasks</span></span><span> </span>[Task] - 一个由 Task 对象构成的数组</li></ul>
<p class="comments-section" >将<span> </span><span ><span >tasks</span></span><span> </span>添加到 Windows 中 JumpList 功能的<span> </span>Tasks<span> </span>分类中。</p>
<p class="comments-section" ><span ><span >tasks</span></span><span> </span>中的<span> </span><span ><span >Task</span></span><span> </span>对象格式如下：</p>
<p class="comments-section" ><span ><span >Task</span></span><span> </span>Object</p>
<ul >
<li >
<span ><span >program</span></span><span> </span>String - 执行程序的路径，通常你应该说明当前程序的路径为<span> </span><span ><span >process.execPath</span></span><span> </span>字段。</li>
<li >
<span ><span >arguments</span></span><span> </span>String - 当<span> </span><span ><span >program</span></span><span> </span>执行时的命令行参数。</li>
<li >
<span ><span >title</span></span><span> </span>String - JumpList 中显示的标题。</li>
<li >
<span ><span >description</span></span><span> </span>String - 对这个任务的描述。</li>
<li >
<span ><span >iconPath</span></span><span> </span>String - JumpList 中显示的图标的绝对路径，可以是一个任意包含一个图标的资源文件。通常来说，你可以通过指明<span> </span><span ><span >process.execPath</span></span><span> </span>来显示程序中的图标。</li>
<li >
<span ><span >iconIndex</span></span><span> </span>Integer - 图标文件中的采用的图标位置。如果一个图标文件包括了多个图标，就需要设置这个值以确定使用的是哪一个图标。 如果这个图标文件中只包含一个图标，那么这个值为 0。</li>
</ul>
<h3 id="appallowntlmcredentialsforalldomainsallow" ><span ><span >app.allowNTLMCredentialsForAllDomains(allow)</span></span></h3>
<ul ><li >
<span ><span >allow</span></span><span> </span>Boolean</li></ul>
<p class="comments-section" >动态设置是否总是为 HTTP NTLM 或 Negotiate 认证发送证书。通常来说，Electron 只会对本地网络（比如和你处在一个域中的计算机）发 送 NTLM / Kerberos 证书。但是假如网络设置得不太好，可能这个自动探测会失效，所以你可以通过这个接口自定义 Electron 对所有 URL 的行为。</p>
<h3 id="appmakesingleinstancecallback" ><span ><span >app.makeSingleInstance(callback)</span></span></h3>
<ul ><li >
<span ><span >callback</span></span><span> </span>Function</li></ul>
<p class="comments-section" >这个方法可以让你的应用在同一时刻最多只会有一个实例，否则你的应用可以被运行多次并产生多个实例。你可以利用这个接口保证只有一个实例正 常运行，其余的实例全部会被终止并退出。</p>
<p class="comments-section" >如果多个实例同时运行，那么第一个被运行的实例中<span> </span><span ><span >callback</span></span><span> </span>会以<span> </span><span ><span >callback(argv, workingDirectory)</span></span>的形式被调用。其余的实例 会被终止。<span> </span><span ><span >argv</span></span><span> </span>是一个包含了这个实例的命令行参数列表的数组，<span ><span >workingDirectory</span></span><span> </span>是这个实例目前的运行目录。通常来说，我们会用通过将应用在 主屏幕上激活，并且取消最小化，来提醒用户这个应用已经被打开了。</p>
<p class="comments-section" >在<span> </span><span ><span >app</span></span><span> </span>的<span> </span><span ><span >ready</span></span><span> </span>事件后，<span ><span >callback</span></span><span> </span>才有可能被调用。</p>
<p class="comments-section" >如果当前实例为第一个实例，那么在这个方法将会返回<span> </span><span ><span >false</span></span><span> </span>来保证它继续运行。否则将会返回<span> </span><span ><span >true</span></span><span> </span>来让它立刻退出。</p>
<p class="comments-section" >在 OS X 中，如果用户通过 Finder、<span ><span >open-file</span></span><span> </span>或者<span> </span><span ><span >open-url</span></span><span> </span>打开应用，系统会强制确保只有一个实例在运行。但是如果用户是通过 命令行打开，这个系统机制会被忽略，所以你仍然需要靠这个方法来保证应用为单实例运行的。</p>
<p class="comments-section" >下面是一个简单的例子。我们可以通过这个例子了解如何确保应用为单实例运行状态。</p>
<pre ><span >var<span > myWindow = </span></span><span >null</span>;

<span >var</span> shouldQuit = app.makeSingleInstance(<span >function<span >(</span>commandLine, workingDirectory</span>) {
  <span >// 当另一个实例运行的时候，这里将会被调用，我们需要激活应用的窗口</span>
  <span >if</span> (myWindow) {
    <span >if</span> (myWindow.isMinimized()) myWindow.restore();
    myWindow.focus();
  }
  <span >return</span> <span >true</span>;
});

<span >// 这个实例是多余的实例，需要退出</span>
<span >if</span> (shouldQuit) {
  app.quit();
  <span >return</span>;
}

<span >// 创建窗口、继续加载应用、应用逻辑等……</span>
app.on(<span >'ready'</span>, <span >function<span >(</span></span>) {
});
</pre>
<h3 id="appsetappusermodelidid-windows" >
<span ><span >app.setAppUserModelId(id)</span></span><span> </span><span >Windows</span>
</h3>
<ul ><li >
<span ><span >id</span></span><span> </span>String</li></ul>
<p class="comments-section" >改变当前应用的<span> </span>Application User Model ID<span> </span>为<span> </span><span ><span >id</span></span>.</p>
<h3 id="appisaeroglassenabled-windows" >
<span ><span >app.isAeroGlassEnabled()</span></span><span> </span><span >Windows</span>
</h3>
<p class="comments-section" >如果<span> </span>DWM composition(Aero Glass) 启用 了，那么这个方法会返回<span> </span><span ><span >true</span></span>，否则是<span> </span><span ><span >false</span></span>。你可以用这个方法来决定是否要开启透明窗口特效，因为如果用户没开启 DWM，那么透明窗 口特效是无效的。</p>
<p class="comments-section" >举个例子：</p>
<pre ><span >let<span > browserOptions = {width:<span ><span > </span></span></span></span><span >1000</span>, height: <span >800</span>};

<span >// 只有平台支持的时候才使用透明窗口</span>
<span >if</span> (process.platform !== <span >'win32'</span> || app.isAeroGlassEnabled()) {
  browserOptions.transparent = <span >true</span>;
  browserOptions.frame = <span >false</span>;
}

<span >// 创建窗口</span>
win = <span >new</span> BrowserWindow(browserOptions);

<span >// 转到某个网页</span>
<span >if</span> (browserOptions.transparent) {
  win.loadURL(<span >'file://'</span> + __dirname + <span >'/index.html'</span>);
} <span >else</span> {
  <span >// 没有透明特效，我们应该用某个只包含基本样式的替代解决方案。</span>
  win.loadURL(<span >'file://'</span> + __dirname + <span >'/fallback.html'</span>);
}
</pre>
<h3 id="appcommandlineappendswitchswitch-value" ><span ><span >app.commandLine.appendSwitch(switch[, value])</span></span></h3>
<p class="comments-section" >通过可选的参数<span> </span><span ><span >value</span></span><span> </span>给 Chromium 中添加一个命令行开关。</p>
<p class="comments-section" ><span >注意</span><span> </span>这个方法不会影响<span> </span><span ><span >process.argv</span></span>，我们通常用这个方法控制一些底层 Chromium 行为。</p>
<h3 id="appcommandlineappendargumentvalue" ><span ><span >app.commandLine.appendArgument(value)</span></span></h3>
<p class="comments-section" >给 Chromium 中直接添加一个命令行参数，这个参数<span> </span><span ><span >value</span></span><span> </span>的引号和格式必须正确。</p>
<p class="comments-section" ><span >注意</span><span> </span>这个方法不会影响<span> </span><span ><span >process.argv</span></span>。</p>
<h3 id="appdockbouncetype-os-x" >
<span ><span >app.dock.bounce([type])</span></span><span> </span><span >OS X</span>
</h3>
<ul ><li >
<span ><span >type</span></span><span> </span>String - 可选参数，可以是<span> </span><span ><span >critical</span></span><span> </span>或<span> </span><span ><span >informational</span></span>。默认为<span> </span><span ><span >informational</span></span>。</li></ul>
<p class="comments-section" >当传入的是<span> </span><span ><span >critical</span></span><span> </span>时，dock 中的应用将会开始弹跳，直到这个应用被激活或者这个请求被取消。</p>
<p class="comments-section" >当传入的是<span> </span><span ><span >informational</span></span><span> </span>时，dock 中的图标只会弹跳一秒钟。但是，这个请求仍然会激活，直到应用被激活或者请求被取消。</p>
<p class="comments-section" >这个方法返回的返回值表示这个请求的 ID。</p>
<h3 id="appdockcancelbounceid-os-x" >
<span ><span >app.dock.cancelBounce(id)</span></span><span> </span><span >OS X</span>
</h3>
<ul ><li >
<span ><span >id</span></span><span> </span>Integer</li></ul>
<p class="comments-section" >取消这个<span> </span><span ><span >id</span></span><span> </span>对应的请求。</p>
<h3 id="appdocksetbadgetext-os-x" >
<span ><span >app.dock.setBadge(text)</span></span><span> </span><span >OS X</span>
</h3>
<ul ><li >
<span ><span >text</span></span><span> </span>String</li></ul>
<p class="comments-section" >设置应用在 dock 中显示的字符串。</p>
<h3 id="appdockgetbadge-os-x" >
<span ><span >app.dock.getBadge()</span></span><span> </span><span >OS X</span>
</h3>
<p class="comments-section" >返回应用在 dock 中显示的字符串。</p>
<h3 id="appdockhide-os-x" >
<span ><span >app.dock.hide()</span></span><span> </span><span >OS X</span>
</h3>
<p class="comments-section" >隐藏应用在 dock 中的图标。</p>
<h3 id="appdockshow-os-x" >
<span ><span >app.dock.show()</span></span><span> </span><span >OS X</span>
</h3>
<p class="comments-section" >显示应用在 dock 中的图标。</p>
<h3 id="appdocksetmenumenu-os-x" >
<span ><span >app.dock.setMenu(menu)</span></span><span> </span><span >OS X</span>
</h3>
<ul ><li >
<span ><span >menu</span></span><span> </span><a href="MENU.html">Menu</a>
</li></ul>
<p class="comments-section" >设置应用的<span> </span><span >dock 菜单</span>.</p>
<h3 id="appdockseticonimage-os-x" >
<span ><span >app.dock.setIcon(image)</span></span><span> </span><span >OS X</span>
</h3>
<ul ><li >
<span ><span >image</span></span><span> </span><a href="NATIVEIMAGE.html">NativeImage</a>
</li></ul>
<p class="comments-section" >设置应用在 dock 中显示的图标。</p></div>
      <div class="prev-next" style="padding:8px;">下一篇：<a class="prev" href="ELECTRONAUTOUPDATERMoKuai.html">Electron autoUpdater 模块</a><br></div>
    
            </div>
        </div> 
      </div>
    </div>
  </body>

<!-- Mirrored from www.dba.cn/book/electron/ZaiZhuJinChengNeiKeYongDeMoKuai/ELECTRONAPPMoKuai.html by HTTrack Website Copier/3.x [XR&CO'2014], Sun, 20 Mar 2022 10:24:09 GMT -->
</html>